package hp.com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.taglibs.standard.lang.jstl.test.beans.PublicBean1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import hp.com.entity.Car;
import hp.com.service.ManagerService;
import hp.com.service.ManagerServiceImpl;
import hp.com.entity.Manager;
import hp.com.entity.Order;
import hp.com.entity.User;
import hp.com.mapper.ManagerMapper;

@Controller
public class ManagerController {
	@Autowired
	private ManagerServiceImpl service;  
	//管理员的登陆
	@RequestMapping("/manager")
	public String login(Manager manager,HttpSession session){
		Manager m = service.login(manager);
		if(m!=null){
			session.setAttribute("musername", m.getMusername());
			return "index";
		}else{
			session.setAttribute("msg", "登陆失败");
			return "redirect:/login.jsp";
		}
	}
	@ResponseBody
	@RequestMapping("/carSelAll")
	//添加前的汽车名称下拉框的回显
	//分页默认第一页
	public PageInfo carSelAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn,7);
		List clist = service.carSelAll();
		PageInfo page = new PageInfo(clist,7);
		return page;
	}
	
	@RequestMapping("/carAdd")
	//添加汽车信息
	@ResponseBody
	public void carAdd(Car car){
		System.out.println(car);
		int a=service.carAdd(car);	
	}
	
	@ResponseBody	
	@RequestMapping("/delCarById")
	//删除汽车信息
	public boolean delCarById(int cid){
		int a=service.delCarById(cid);
		if(a>0){
			return true;
		}else{
			return false;
		}
	}
	
	@ResponseBody
	@RequestMapping("/selcarById")
	//查询回显
	public Car selcarById(int cid){
		Car car=service.selcarById(cid);
		return car;
	}
	
	//修改汽车信息
	@RequestMapping("/carsUpd")
	@ResponseBody
	public boolean carUpd(String cname,String ccard,double cmoney,String cstate,int cid){
		Car car=new Car();
		car.setCid(cid);
		car.setCmoney(cmoney);
		car.setCname(cname);
		car.setCstate(cstate);
		car.setCcard(ccard);
		System.out.println(car);
		int i = service.carUpd(car);
		System.out.println(i);
		if(i>0){
			return true;
		}else{
			return false;
		}
	}
	//通过id查询管理员
	@RequestMapping("/gselcarById")
	@ResponseBody
	public Manager gselcarById(int mid){
		Manager manager = service.gselcarById(mid);
		return manager;
	}
	//通过id修改管理员密码
	@RequestMapping("/updateguanli")
	@ResponseBody
	public int gupdate(int mid,String mpassword){
		Manager manager=new Manager();
		manager.setMid(mid);
		manager.setMpassword(mpassword);
		System.out.println(manager);
		System.out.println(manager);
		int a = service.gupdate(manager);
		System.out.println(a);
		return a;
	}
	@ResponseBody
	@RequestMapping("/managerSelAll")
	//查询管理员
	public PageInfo managerSelAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn,7);
		List clist = service.managerSelAll();
		PageInfo page = new PageInfo(clist,7);
		return page;
	}
	
	@ResponseBody
	@RequestMapping("/managerAdd")
	//添加管理员
	public boolean managerAdd(Manager manager){
		int i = service.managerAdd(manager);
		if(i>0){
			return true;
		}else{
			return false;
		}
	}
	
	@ResponseBody
	@RequestMapping("/delmanagerById")
	//删除管理员信息
	public boolean delmanagerById(int id){
		int i = service.delmanagerById(id);
		if(i>0){
			return true;
		}else{
			return false;
		}
	}
	
	@RequestMapping("/userSelAll")
	@ResponseBody
	//查询用户
	public PageInfo usersSelAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn,7);
		List clist = service.userSelAll();
		PageInfo page = new PageInfo(clist,7);
		return page;
	}
	//添加用户
	@RequestMapping("/usersAdd")
	@ResponseBody
	public boolean userAdd(User user){
		int i = service.userAdd(user);
		if(i>0){
			return true;
		}
		return false;
	}
	//删除用户
	@RequestMapping("/delusersById")
	@ResponseBody
	public boolean deluserById(int id){
		int i = service.deluserById(id);
		if(i>0){
			return true;
		}
		return false;
	}
	
	//显示用户订单
	@RequestMapping("/selordersById")
	@ResponseBody
	public List<Order> selorderById(int uid,HttpServletRequest request){
		System.out.println(uid);
		List<Order> list = service.selorderById(uid);
		for (Order order : list) {
			System.out.println(order);
		}
		return list;
	}
	
	@RequestMapping("/orderSelAll")
	@ResponseBody
	//查询订单信息
	public PageInfo ordersSelAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn,7);
		List<Order> clist = service.orderSelAll();
		PageInfo page = new PageInfo(clist,7);
		return page;
	}
}
